package com.wx.springboot.step9back.controller;

import com.wx.springboot.step9back.common.api.CommonResult;
import com.wx.springboot.step9back.model.Page;
import com.wx.springboot.step9back.model.Team;
import com.wx.springboot.step9back.service.TeamService;
import com.wx.springboot.step9back.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private TeamService teamService;
    @Autowired
    private UserService userService;

    @PreAuthorize("hasAuthority('wx:team:read')")
    @GetMapping("/getTeamList")
    public CommonResult getTeamList(){
        HashMap<String, Object> map = new HashMap<>();
        List<Team> teams = teamService.getTeamList();
        map.put("data",teams);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:team:read')")
    @GetMapping("/getSelectList")
    public CommonResult getSelectList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<Team> teams = teamService.getSelectList(data);
        int count = teamService.getSelectCount(data);
        Page page = new Page();
        page.setList(teams);
        page.setTotal(count);
        map.put("list",teams);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:team:read')")
    @GetMapping("/getTeamByTeamCode")
    public CommonResult getTeamByTeamCode(@RequestParam String teamCode){
        HashMap<String, Object> map = new HashMap<>();
        Team team = teamService.getTeamByTeamCode(teamCode);
        map.put("data",team);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:team:create')")
    @PostMapping("/addTeam")
    public CommonResult addTeam(@RequestBody Team team){
        HashMap<String, String> map = new HashMap<>();
        String result = teamService.addTeam(team);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:team:update')")
    @PostMapping("/updateTeam")
    public CommonResult updateTeam(@RequestBody Team team){
        HashMap<String, Object> map = new HashMap<>();
        int result = teamService.updateTeam(team);
        if (result == 1){
            userService.updateUserRole(team.getTeamCode(),team.getTeamLeader());
        }
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:team:delete')")
    @PostMapping("/delTeam")
    public CommonResult delTeam(@RequestBody Team team){
        HashMap<String, Integer> map = new HashMap<>();
        int result = teamService.delTeam(team);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:team:update')")
    @PostMapping("/updateState")
    public CommonResult updateState(@RequestBody Team team){
        HashMap<String, Object> map = new HashMap<>();
        int result = teamService.updateState(team);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:team:delete')")
    @PostMapping("/batchDeletion")
    public CommonResult batchDeletion(@RequestParam List<Integer> ids){
        HashMap<String, Object> map = new HashMap<>();
        String result = teamService.batchDeletion(ids);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:team:update')")
    @PostMapping("/updateLeader")
    public CommonResult updateLeader(@RequestParam Integer id,@RequestParam String teamCode){
        HashMap<String, Object> map = new HashMap<>();
        String date = teamService.removeLeader(teamCode);
        String param = userService.updateLeaderToStu(id);
        String result;
        if (date == "success" && param == "success"){
            result = "success";
        } else {
            result = "false";
        }
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:team:read')")
    @PostMapping("/getCheckTeam")
    public CommonResult getCheckTeam(@RequestParam List<Integer> ids){
        HashMap<String, Object> map = new HashMap<>();
        List<Team> teams = teamService.getCheckTeam(ids);
        map.put("data",teams);
        return CommonResult.success(map);
    }


}
